package com.fr;

import org.junit.Test;

/**
*作者：furong
*日期：2016年12月22日
*时间：下午2:34:37
*/
public class search_a_2d_matrix {
    @Test
    public void testA() {
        int matrix[][] = {};
        int target = 11;
        System.out.println(searchMatrix(matrix, target));
    }

    public int searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        if (m <= 0) {
            return 0;
        }
        int n = matrix[0].length;
        if (n <= 0) {
            return 0;
        }
        int a = 0;
        int b = 0;
        int count = 0;
        while (a < m) {
            if (b >= n) {
                a++;
                b = 0;
                continue;
            }
            if (matrix[a][b] == target) {
                a++;
                n = b;
                count++;
                b = 0;
            } else if (matrix[a][b] < target) {
                b++;
            } else {
                a++;
                n = b;
                b = 0;
            }
        }
        return count;
    }

    @Test
    public void testB() {
        int A[] = { 1, 2, 1, 3, 4, 5, 7, 6 };
        System.out.println(findPeak(A));
    }

    public int findPeak(int[] a) {
        if (a.length < 2) {
            return -1;
        }
        for (int i = 1; i < a.length - 1; i++) {
            if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
                return i;
            }
        }
        return -1;
    }
}
